/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package byzantine.controls;

import byzantine.ByzantineModel;
import byzantine.General;
import byzantine.controls.listeners.RemoveGeneralListener;
import byzantine.controls.listeners.SelectGeneralListener;
import byzantine.views.View;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 *
 * @author rpathan
 */
public class SelectedGeneralControls extends JPanel implements View {

    //private int width;
    //private int height;
    private int width = 250;
    private int height = 250;
    //private int imageWidth;
    //private int imageHeight;
    private int imageWidth = 87;
    private int imageHeight  = 137;
    //private int labelToComponentsSpacing;
    //private int imageToButtonSpacing;
    //private int componentSpacing;
    private int labelToComponentsSpacing = 5;
    private int imageToButtonSpacing = 5;
    private int componentSpacing = 8;
    private ByzantineModel model;
    private JLabel panelName;
    private JLabel imageLabel;
    private ImageIcon noTargetSelectedIcon;
    private ImageIcon commanderIcon;
    private ImageIcon commanderRebelIcon;
    private ImageIcon generalIcon;
    private ImageIcon generalRebelIcon;
    private JComboBox generalsBox;
    private JButton removeButton;
    private String selectedItem;
    Box box;

    public SelectedGeneralControls(ByzantineModel model) {
        this.model = model;
        model.addObserver(this);
        /*
        Toolkit t = Toolkit.getDefaultToolkit();
        Dimension screenSize = t.getScreenSize();
        width = (int)((screenSize.width - 10) * 0.25);
        height = (int)((screenSize.height - 100) * 0.44);
        imageWidth = (int)(width * 0.50);
        imageHeight = (int)(height * 0.69);
        labelToComponentsSpacing = (int)(height * 0.0375);
        imageToButtonSpacing = (int)(height * 0.0375);
        componentSpacing = (int)(height * 0.02);
         */
        initializeControls();
    }

    private void initializeControls() {

        panelName = new JLabel("Selected General");
        panelName.setAlignmentX(Component.CENTER_ALIGNMENT);
        panelName.setAlignmentY(Component.CENTER_ALIGNMENT);
        panelName.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 16));

        noTargetSelectedIcon = new ImageIcon(getClass().getResource("/byzantine/resources/NoTargetSelected.jpg"));
        Image noTargetSelectedScaled = noTargetSelectedIcon.getImage().getScaledInstance(imageWidth, imageHeight, Image.SCALE_SMOOTH);
        noTargetSelectedIcon.setImage(noTargetSelectedScaled);

        commanderIcon = new ImageIcon(getClass().getResource("/byzantine/resources/Commander.jpg"));
        Image commanderScaled = commanderIcon.getImage().getScaledInstance(imageWidth, imageHeight, Image.SCALE_SMOOTH);
        commanderIcon.setImage(commanderScaled);

        commanderRebelIcon = new ImageIcon(getClass().getResource("/byzantine/resources/Commander_Rebel.jpg"));
        Image commanderRebelScaled = commanderRebelIcon.getImage().getScaledInstance(imageWidth, imageHeight, Image.SCALE_SMOOTH);
        commanderRebelIcon.setImage(commanderRebelScaled);

        generalIcon = new ImageIcon(getClass().getResource("/byzantine/resources/General.jpg"));
        Image generalScaled = generalIcon.getImage().getScaledInstance(imageWidth, imageHeight, Image.SCALE_SMOOTH);
        generalIcon.setImage(generalScaled);

        generalRebelIcon = new ImageIcon(getClass().getResource("/byzantine/resources/General_Rebel.jpg"));
        Image generalRebelScaled = generalRebelIcon.getImage().getScaledInstance(imageWidth, imageHeight, Image.SCALE_SMOOTH);
        generalRebelIcon.setImage(generalRebelScaled);

        imageLabel = new JLabel(noTargetSelectedIcon);
        imageLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
        imageLabel.setAlignmentY(Component.CENTER_ALIGNMENT);
        imageLabel.setToolTipText(ByzantineModel.NO_TARGET_SELECTED);

        generalsBox = new JComboBox();
        generalsBox.addItem("No_Selection");
        for (int i = 0; i < model.getNumGenerals(); i++) {
            General general = model.getGeneral(i);
            generalsBox.addItem(general.getID().toString() + " - " + general.getType());
        }
        generalsBox.setAlignmentX(Component.CENTER_ALIGNMENT);
        generalsBox.setAlignmentY(Component.CENTER_ALIGNMENT);
        generalsBox.addItemListener(new SelectGeneralListener(this, model));
        generalsBox.setEnabled(true);

        selectedItem = generalsBox.getSelectedItem().toString();

        removeButton = new JButton("Remove Selected General");
        removeButton.setAlignmentX(Component.CENTER_ALIGNMENT);
        removeButton.setAlignmentY(Component.CENTER_ALIGNMENT);
        removeButton.addActionListener(new RemoveGeneralListener(model));
        removeButton.setEnabled(false);

        box = Box.createVerticalBox();
        box.add(panelName);
        box.add(Box.createVerticalStrut(labelToComponentsSpacing));
        box.add(imageLabel);
        box.add(Box.createVerticalStrut(imageToButtonSpacing));
        box.add(generalsBox);
        box.add(Box.createVerticalStrut(componentSpacing));
        box.add(removeButton);

        this.add(box);

        setBorder(BorderFactory.createEtchedBorder());
    }

    public String getSelectedItem() {
        return selectedItem;
    }

    private void update() {
        if (model.getSelectedGeneral() != null && model.isSimulationRunning() == false) {
            removeButton.setEnabled(true);
        } else if (model.getSelectedGeneral() == null || model.isSimulationRunning() == true) {
            removeButton.setEnabled(false);
        }

        if (model.getSelectedGeneral() == null) {
            imageLabel.setIcon(noTargetSelectedIcon);
            imageLabel.setToolTipText(ByzantineModel.NO_TARGET_SELECTED);
        } else {

            String type = model.getSelectedGeneral().getType();

            if (type.equals(ByzantineModel.COMMANDER)) {
                imageLabel.setIcon(commanderIcon);
                imageLabel.setToolTipText(ByzantineModel.COMMANDER);
            } else if (type.equals(ByzantineModel.COMMANDER_REBEL)) {
                imageLabel.setIcon(commanderRebelIcon);
                imageLabel.setToolTipText(ByzantineModel.COMMANDER_REBEL);
            } else if (type.equals(ByzantineModel.GENERAL)) {
                imageLabel.setIcon(generalIcon);
                imageLabel.setToolTipText(ByzantineModel.GENERAL);
            } else if (type.equals(ByzantineModel.GENERAL_REBEL)) {
                imageLabel.setIcon(generalRebelIcon);
                imageLabel.setToolTipText(ByzantineModel.GENERAL_REBEL);
            }
        }

        if (model.hasNumGeneralsChanged() == true) {

            box.removeAll();
            int selectedIndex = generalsBox.getSelectedIndex();
            int oldItemCount = generalsBox.getItemCount();

            generalsBox.removeItemListener(generalsBox.getItemListeners()[0]);
            generalsBox = new JComboBox();
            generalsBox.addItem("No_Selection");
            for (int i = 0; i < model.getNumGenerals(); i++) {
                General general = model.getGeneral(i);
                generalsBox.addItem(general.getID().toString() + " - " + general.getType());
            }
            if (oldItemCount > generalsBox.getItemCount()) {
                selectedIndex = 0;
            }
            generalsBox.setAlignmentX(Component.CENTER_ALIGNMENT);
            generalsBox.setAlignmentY(Component.CENTER_ALIGNMENT);
            generalsBox.addItemListener(new SelectGeneralListener(this, model));
            generalsBox.setSelectedIndex(selectedIndex);
            generalsBox.setEnabled(true);
            box.add(panelName);
            box.add(Box.createVerticalStrut(labelToComponentsSpacing));
            box.add(imageLabel);
            box.add(Box.createVerticalStrut(imageToButtonSpacing));
            box.add(generalsBox);
            box.add(Box.createVerticalStrut(componentSpacing));
            box.add(removeButton);

            selectedItem = generalsBox.getSelectedItem().toString();
        }


    }

    public void valueChanged() {
        if (model.hasSelectedGeneralChanged()
                || model.hasNumGeneralsChanged()
                || model.hasSimulationStatusChanged()) {
            update();
        }
    }

    @Override
    public Dimension getPreferredSize() {

        return new Dimension(width, height);
    }
}
